package com.ymtms.applicationsystem.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ymtms.applicationsystem.dao.WageMapper;
import com.ymtms.applicationsystem.exception.WageException;
import com.ymtms.applicationsystem.pojo.Wage;
import com.ymtms.applicationsystem.service.WageService;
import com.ymtms.applicationsystem.vo.RequestBean;
import com.ymtms.applicationsystem.vo.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @auther: autumn-luo
 * @description:
 * @createTime: 2019-10-29 10:54
 */
@Log4j2
@Service
public class WageServiceImpl implements WageService {

    @Autowired
    WageMapper wageMapper;

    @Override
    public int wageDel(Integer wid) {
        int delResult = wageMapper.wageDelSoft(wid);
        if (delResult == 1) {
            return delResult;
        } else {
            throw new WageException("工资条信息删除失败" + EmpServiceImpl.class.getName());
        }
    }

    @Override
    public int wageUpdate(Wage wage) {
        wage.setFlag(0);
        int wageUpdateResult = wageMapper.updateByPrimaryKey(wage);
        if (wageUpdateResult == 0) {
            throw new WageException("职员工资条更新失败");
        } else {
            return wageUpdateResult;
        }
    }

    @Override
    public int wageAdd(Wage wage) {
        wage.setFlag(0);
        int wageAddResult = wageMapper.insert(wage);
        if (wageAddResult == 0) {
            throw new WageException("职员工资条插入错误");
        } else {
            return wageAddResult;
        }

    }

    @Override
    public String wageNameByCode(String employeeCode) {
        String wageName = wageMapper.selectNameByCode(employeeCode);
        if (wageName == null || wageName == "") {
            throw new WageException("没有这个员工，请输入正确的员工编号");
        } else {
            return wageName;
        }

    }


    @Override
    public Result<Wage> wageAll(RequestBean requestBean, String empWageSeekInfo) {
        log.info("empWageSeekInfo");
        // 获取当前页和页大小
        Integer currentPage = requestBean.getPage();
        Integer pageSize = requestBean.getLimit();

        // 判断是否有排序,有则生成符合pagehelper的排序字符串
        String sidx = requestBean.getSidx(); // 排序参数
        String sord = requestBean.getSord(); // 排序规则

        String orderBy = "";
        // pagehelper静态设置页属性方法，它会拦截下面的第一个查询！第二个无效。
        if (checkOrderBy(sidx, sord)) {
            sidx = upperCharToUnderLine(sidx);
            orderBy = sidx + " " + sord;
            PageHelper.startPage(currentPage, pageSize, orderBy);
        } else {
            // 设置当前页和页大小
            PageHelper.startPage(currentPage, pageSize);
        }
        // 查询结果传入排序
        List<Wage> wage = wageMapper.selectWagesAll(empWageSeekInfo);

        // 利用当前页和页大小配置pageinfo
        PageInfo<Wage> pageList = new PageInfo<>(wage);
        // 新建一个封装类对象，设置参数，传入查询得到的数据
        Result<Wage> wageInfo = new Result<>();
        if (wage.size() == 0) {
            // 存入查询数据
            wageInfo.setData(null);
            // 存入总条数
            wageInfo.setCount(1);
        }else {
            // 存入查询数据
            wageInfo.setData(wage);
            // 存入总条数
            wageInfo.setCount(pageList.getTotal());
        }


        return wageInfo;
    }


    // 判断是否非空
    private boolean checkOrderBy(String sidx, String sord) {
        if (sidx != null && sord != null
                && sidx.trim().length() > 0
                && sord.trim().length() > 0) {
            return true;
        }
        return false;
    }

    // 大写转换为下划线+小写
    public static String upperCharToUnderLine(String param) {
        Pattern p = Pattern.compile("[A-Z]");
        if (param == null || param.equals("")) {
            return "";
        }
        StringBuilder builder = new StringBuilder(param);
        Matcher mc = p.matcher(param);
        int i = 0;
        while (mc.find()) {
            builder.replace(mc.start() + i, mc.end() + i, "_" + mc.group().toLowerCase());
            i++;
        }

        if ('_' == builder.charAt(0)) {
            builder.deleteCharAt(0);
        }
        return builder.toString();
    }
}


